/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package presentation.proportional;

import customGuiControl.ImagePanel;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.LayoutManager;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import org.jdesktop.swingx.StackLayout;

/**
 *
 * @author emilio
 */
public class offsetLayout implements LayoutManager {
    private Float marginTopPercent=0f;
    private Float marginLeftPercent=0f;
    private JPanel stackElement=null;
    private Component c=null;
    private Float ScaleFatt=1.1f;
    private int imgWidth=0;
    private int imgHeight=0;
    private Float imgScaleFatt;

    public offsetLayout(Float marginTopPercent, Float marginLeftPercent,JPanel stackParent)
    {
        this.marginTopPercent=marginTopPercent;
        this.marginLeftPercent=marginLeftPercent;
        this.stackElement=stackParent;
    }

    public offsetLayout(Float marginTopPercent, Float marginLeftPercent, float scaleFatt,JPanel stackParent)
    {
        this.marginTopPercent=marginTopPercent;
        this.marginLeftPercent=marginLeftPercent;
        this.stackElement=stackParent;
        this.ScaleFatt=scaleFatt;
    }
    public void addLayoutComponent(String name, Component comp) {
       
    }

    public void removeLayoutComponent(Component comp) {
      
    }

    public Dimension preferredLayoutSize(Container parent) {
       return new Dimension(0,0);
    }

    public Dimension minimumLayoutSize(Container parent) {
        return new Dimension(0,0);
    }

    public void layoutContainer(Container parent) {
        ArrayList<Integer> absolute= this.getAbsoluteOffset(parent);
        if(this.c==null)
        {
            this.c= parent.getComponent(0);
            parent.remove(c);
            if(c.getClass().equals(ImagePanel.class))
            {
                ImagePanel img = (ImagePanel) c;
                ImageIcon tempImg= new ImageIcon(img.getPathImg());
                this.imgHeight=tempImg.getIconHeight();
                this.imgWidth=tempImg.getIconWidth();
                this.imgScaleFatt=Float.valueOf(this.imgHeight)/Float.valueOf(this.imgWidth);
            }
            if(c.getClass().equals(JPanel.class))
            {
                JPanel p= (JPanel)c;
                this.imgHeight=c.getPreferredSize().height;
                this.imgWidth=c.getPreferredSize().width;
                this.imgScaleFatt=Float.valueOf(this.imgHeight)/Float.valueOf(this.imgWidth);
            }
        }
        int imageW= Float.valueOf(parent.getWidth()*this.ScaleFatt).intValue();
        int imageH= Float.valueOf(imageW*this.imgScaleFatt).intValue();
        c.setBounds(absolute.get(0), absolute.get(1), imageW,imageH);
        this.stackElement.add(c);

    }

    public ArrayList<Integer> getAbsoluteOffset(Container parent){
        ArrayList<Integer> AbsOffset= new ArrayList<Integer>();
        int x=parent.getWidth()*this.marginLeftPercent.intValue()/100;
        int y=parent.getHeight()* this.marginTopPercent.intValue()/100;
        while(!parent.getClass().equals(JRootPane.class))
        {
            x+=parent.getX();
            x-=parent.getInsets().left;
            y+=parent.getY();
            y-=(parent.getInsets().top);
            //System.out.println("individuato un parent "+parent.toString()+String.valueOf(parent.getY()));
            parent=parent.getParent();
            if(parent==null)
            {
                System.out.println("");
            }
        }
        AbsOffset.add(x);
        AbsOffset.add(y);
        return AbsOffset;
    }

}
